﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace community
{
    public partial class childcheck : Form
    {
        public static string child_JMBH;
        string sql ;
        int y;
        int m;
        int d;
        DateTime dt_now;
        DateTime dt_old;
        public childcheck()
        {
            InitializeComponent();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                textBox1.Enabled = true;
                textBox1.Focus();
            }
            else
            {
                textBox1.Enabled = false;
                textBox1.Text = "";
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
                textBox2.Enabled = true;
            else
            {
                textBox2.Enabled = false;
                textBox2.Text = "";
            }
        }

        private void textBox2_Click(object sender, EventArgs e)
        {
            dataGridView2.Visible = true;
            SqlConnection  cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string sqlStr = "select COMMUNITYCODE_CHR as 社区编号,COMMUNITYNAME_VCHR as 社区名称  from t_cs_community  where  CURRENTCS_INT=1";
            SqlDataAdapter dt = new SqlDataAdapter(sqlStr, cn);
            DataSet ds = new DataSet();
            dt.Fill(ds);
            this.dataGridView2.DataSource = ds.Tables[0];
            dataGridView2.ColumnHeadersHeight = 39;
            dataGridView2.Columns[0].Width =150;
            dataGridView2.Columns[1].Width = 150;
            cn.Close();
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                textBox2.Text= dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString();
                if (textBox2.Text != null) dataGridView2.Visible = false;
            }
        }

        private void childcheck_Load(object sender, EventArgs e)
        {
            y = DateTime.Now.Year;
            m = DateTime.Now.Month;
            d = DateTime.Now.Day;
            dt_now = DateTime.Now;
            //CONVERT(char(10),dt_now,111);
            dt_old = new DateTime(y - 7, m, d);
            //MessageBox.Show(dt_old.ToString());
            sql = "select inhabitantcode_chr as 儿童编号,residentname_chr as 儿童姓名,communityname_chr as 所属社区,sex_chr as 儿童性别,birth_dat as 出生日期, identitycard_chr as 身份证号 from T_CS_INHABITANTRECORD_NEW where birth_dat between '" + dt_old + "' and '" + dt_now + "'";
            newdatagridview1(sql);
        }
        private void newdatagridview1(string str)
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();
            SqlDataAdapter dt = new SqlDataAdapter(str,cn);
            DataSet ds = new DataSet();
            dt.Fill(ds);
            dataGridView1.DataSource=ds.Tables[0];
            cn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            sql = "select inhabitantcode_chr as 儿童编号,residentname_chr as 儿童姓名,communityname_chr as 所属社区,sex_chr as 儿童性别,birth_dat as 出生日期, identitycard_chr as 身份证号 from T_CS_INHABITANTRECORD_NEW where birth_dat between '" + dt_old + "' and '" + dt_now + "'";
            
            if((checkBox1.Checked==true) && textBox1.Text=="" )
            {
                MessageBox.Show("请输入儿童姓名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            
            }
            if((checkBox2.Checked==true)&& textBox2.Text=="")
            {
                MessageBox.Show("请输入检索社区", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if ((checkBox3.Checked == true) && (dateTimePicker1.Text == "") || (dateTimePicker2.Text == ""))
            {
                MessageBox.Show("请输入出生日期", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if (checkBox1.Checked)
                sql += " and residentname_chr='" + textBox1.Text.Trim() + "'";
            if (checkBox2.Checked)
                sql += " and communityname_chr='" + textBox2.Text.Trim() + "'";
            if (checkBox3.Checked)
                sql += "  and birth_dat between '" + Convert.ToDateTime(dateTimePicker1.Text) + "' and '" + Convert.ToDateTime(dateTimePicker2.Text) + "'";
            newdatagridview1(sql);
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                child_JMBH = "";
                child_JMBH= dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            }
            this.Close();
        }

    }
}
